﻿using System.Configuration;
using System.Web;
using EPiServer.DataAbstraction;
using System;
using EPiServer.Web;
using EPiServer;

namespace EPiSugar.Web
{
    public class InitializerModule : IHttpModule
    {
        static readonly object syncLock = new object();
        #region IHttpModule Members

        public void Init(HttpApplication context)
        {
            InitializationModule.FirstBeginRequest += FirstRequest;
        }

        public void FirstRequest(object sender, EventArgs args)
        {
            lock (syncLock)
            {
                if (Current.Model != null)
                    return;

                var config = ConfigurationManager.GetSection("epiSugar") as Configuration.EPiSugarSection;
                var pageTypes = PageType.List();
                Current.Initialize(config, pageTypes, DataFactory.Instance);
            }
        }

        public void Dispose()
        {
        }

        #endregion
    }
}
